Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Encapsulation → Protected variable

Encapsulation

Protected variable

Python doesn't have a built-in "protected" access modifier like some languages (e.g., C++, Java) do. The concept of protection in Python relies on naming conventions and trust rather than strict enforcement by the interpreter. A protected member is indicated by a single underscore prefix (`_variableName`). This signals to other developers that the attribute is intended for internal use within the class and its subclasses, but it's not truly inaccessible. Let's illustrate this with examples: Example 1: Illustrating the "protected" convention
Python protected implementation example class MyClass: def __init__(self, value): self._protected_var = value # Protected variable def get_protected_var(self): return self._protected_var def set_protected_var(self, new_value): self._protected_var = new_value my_obj = MyClass(10) print(my_obj.get_protected_var()) # Accessing using a getter method - recommended print(my_obj._protected_var) # Direct access - works but discouraged my_obj.set_protected_var(20) # Modifying using a setter method - recommended print(my_obj.get_protected_var()) # Although technically accessible, direct access like my_obj._protected_var is considered bad practice. It violates the implied intention of the underscore.

Output

10 10 20
In this example, `_protected_var` is considered protected. While we *can* directly access and modify it, the use of getter (`get_protected_var`) and setter (`set_protected_var`) methods is strongly encouraged. This allows for controlled access and potential validation or other actions within the setter.
Example 2: Inheritance and Protected Members Protected members are accessible from subclasses:
Inheritance and Protected Members example class ParentClass: def __init__(self, value): self._protected_data = value def display_protected(self): print(f"Protected data in Parent: {self._protected_data}") class ChildClass(ParentClass): def __init__(self, value, extra): super().__init__(value) self._new_data = extra # Child's own protected attribute print(f"Access from child: {self._protected_data}") # Accessing parent's protected member child_obj = ChildClass(50, "Hello") child_obj.display_protected() print(child_obj._protected_data) # Direct access (though getter is preferred)

Output

Access from child: 50 Protected data in Parent: 50 50
The `ChildClass` can access and modify `_protected_data` inherited from `ParentClass`. This is a key characteristic of "protected" access – it's intended to be accessible within the class hierarchy.
In summary: Python's "protected" members are a convention, not a strict enforcement mechanism. The single underscore (`_`) indicates the intent to restrict direct access from outside the class and its subclasses, but it's not truly enforced. Using getter and setter methods is the best practice to maintain this intended protection and enables better control and flexibility. Double underscores (`__`) lead to name mangling, making direct access more difficult, but it's not a robust solution for true privacy. Focus on clear code design and documentation rather than relying on weak enforcement mechanisms.

Tutorials